const { MicroRouter } = require('../../model');

// 路由模型
/**
 * @param {string} title 菜单名称
 * @param {string} path 路由路径
 * @param {string} icon 路由图标
 * @param {string} status 路由状态
 * @param {string} orderNum 排序
 * @param {string} parentId 父节点id
 * @param {object} meta meta信息
 * @param {*} req
 * @param {*} res
 */

// 添加路由
const addRouter = async (req, res) => {
  console.log('添加路由');
  const { path = '', icon = '', status = '1', orderNum = 0, parentId, title, meta = {}, type = '1', microAppId } = req.body;

  if (!path) {
    res.send({
      errcode: 1,
      msg: 'path和microAppId不能为空',
      success: false,
    });
  }

  console.log('microAppId', microAppId, parentId);
  try {
    const [router, created] = await MicroRouter.findOrCreate({
      where: {
        path,
      },
      defaults: {
        path,
        icon,
        status,
        orderNum,
        parentId,
        title,
        meta,
        type,
        microAppId,
      },
    });
    if (created) {
      res.json({
        errcode: 0,
        msg: '成功',
        data: router,
        success: true,
      });
    } else {
      res.send({
        errcode: 1,
        msg: 'path已存在，请勿重复添加',
        success: false,
      });
    }
  } catch (error) {
    res.send({
      errcode: 1,
      msg: '失败' + error,
      success: false,
    });
  }
};

// 编辑路由
const editRouter = async (req, res) => {
  const { id, path = '', icon = '', status = 1, orderNum = 0, parentId = '', title, meta, type = '1', microAppId } = req.body;

  if (!id || !path) {
    res.send({
      errcode: 1,
      msg: 'path和microAppId不能为空',
      success: false,
    });
  }
  console.log('编辑路由', { id, path, icon, status, orderNum, parentId, title, meta, type });
  try {
    const router = await MicroRouter.update(
      { path, icon, status, orderNum, parentId, meta, title, type, microAppId },
      {
        where: {
          id,
        },
      }
    );
    if (router) {
      res.json({
        errcode: 0,
        msg: '成功',
        success: true,
        data: router,
      });
    } else {
      res.json({
        errcode: 1,
        msg: '失败',
        success: false,
      });
    }
  } catch (error) {
    res.send({
      errcode: 1,
      msg: '失败' + error,
      success: false,
    });
  }
};

// 删除路由
const deleteRouter = async (req, res) => {
  console.log('删除路由');
  const { id } = req.body;
  try {
    const router = await MicroRouter.destroy({
      where: {
        id,
      },
    });
    if (router) {
      res.json({
        errcode: 0,
        msg: '成功',
        success: true,
      });
    } else {
      res.json({
        errcode: 1,
        msg: '失败',
        success: false,
      });
    }
  } catch (error) {
    res.send({
      errcode: 1,
      msg: '失败' + error,
      success: false,
    });
  }
};

// 路由列表
const routerList = async (req, res) => {
  console.log('路由列表');
  try {
    const routers = await MicroRouter.findAndCountAll();
    if (!routers) {
      res.json({
        errcode: 1,
        msg: '失败',
        success: false,
      });
      return;
    }
    res.json({
      errcode: 0,
      msg: '成功',
      data: routers,
      success: true,
    });
  } catch (error) {
    res.send({
      errcode: 1,
      msg: '失败' + error,
      success: false,
    });
  }
};

/**
 * 获取路由详情
 * @param {*} req
 * @param {*} res
 */
const routerDetail = async (req, res) => {
  try {
    const { id } = req.body;
    if (!id) {
      res.send({
        errcode: 1,
        msg: '参数错误',
        success: false,
      });
      return;
    } else {
      const result = await MicroRouter.findOne({
        where: {
          id,
        },
        attributes: ['id', 'title', 'path', 'icon', 'status', 'type', 'parentId', 'microAppId'],
      });
      if (result) {
        res.send({
          errcode: 0,
          msg: '获取成功',
          success: true,
          data: result,
        });
      } else {
        res.send({
          errcode: 1,
          msg: '获取失败',
          success: false,
        });
      }
    }
  } catch (error) {
    res.send({
      errcode: 1,
      msg: '失败' + error,
      success: false,
    });
  }
};

module.exports = { addRouter, editRouter, deleteRouter, routerList, routerDetail };
